import { getToken, setToken, removeToken } from '@/utils/auth'

const state = {
    token: getToken(),
    roles: [],
    modulesName: ""
}

const mutations = {
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_ROLES: (state, roles) => {
        state.roles = roles
    },
    SET_MODULESNAME: (state, name) => {
        state.modulesName = name
    }
};

const actions = {
    login({ commit }, userInfo) {
        const { username } = userInfo;
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                if (username === "admin" || username === "jyjgl" || username === "jyj" || username === "xxgl" || username === "xx") {
                    commit("SET_TOKEN", username);
                    commit("SET_MODULESNAME", username)
                    setToken(username)
                    resolve()
                } else {
                    reject("用户名，密码错误")
                }
            }, 1000)
        })
    },
    // get user info 
    getInfo({ commit, state }) {
        return new Promise((resolve) => {
            setTimeout(() => {
                const roles = state.token === "admin" ? ['admin'] : ['editor'];
                commit("SET_ROLES", roles)
                commit("SET_MODULESNAME", state.token)
                resolve({ roles })
            }, 1000)
        })
    },
    // reset token
    resetToken({ commit }) {
        return new Promise(resolve => {
            commit("SET_TOKEN", "");
            commit("SET_ROLES", []);
            removeToken();
            resolve()
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}